﻿using Lis.IDAL;
using Lis.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lis.KISSFRSTDAL
{
    public class UserCirculateDal : BaseDal<UserCirculate>, IUserCirculateDal
    {
        #region 01，根据读者类型和典藏地查询流通权限+IEnumerable<UserCirculate> GetEntitiesByUserType(string userType, string collectionCode)
        /// <summary>
        /// 根据读者类型和典藏地查询流通权限
        /// </summary>
        /// <param name="userType">读者类型</param>
        /// <param name="collectionCode">典藏地</param>
        /// <returns></returns>
        public IEnumerable<UserCirculate> GetEntitiesByUserType(string userType, string collectionCode)
        {
            return DB.QueryEntities<UserCirculate>("Lis.UserCirculate.Select.OneByIdAndCollection", new { DZLX = userType, DCDDM = collectionCode });
        }
        #endregion
        #region 02,根读者类型查询流通权限+IEnumerable<UserCirculate> GetEntitiesByUserType(string userType)
        /// <summary>
        /// 根读者类型查询流通权限
        /// </summary>
        /// <param name="userType">读者类型</param>
        /// <returns></returns>
        public IEnumerable<UserCirculate> GetEntitiesByUserType(string userType)
        {
            return DB.QueryEntities<UserCirculate>("Lis.UserCirculate.Select.OneById", new { DZLX = userType });
        }
        #endregion

        #region 03，分页查询+override IEnumerable<UserCirculate> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<UserCirculate, bool>> whereLambda, System.Linq.Expressions.Expression<Func<UserCirculate, S>> orderByLambda)
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="total">总条数</param>
        /// <param name="isAsc">是否倒序</param>
        /// <param name="whereLambda">查询lambda</param>
        /// <param name="orderByLambda">排序lambda</param>
        /// <returns></returns>
        public override IQueryable<UserCirculate> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<UserCirculate, bool>> whereLambda, System.Linq.Expressions.Expression<Func<UserCirculate, S>> orderByLambda)
        {
            var list = DB.QueryEntities<UserCirculate>("Lis.UserCirculate.Select.OneById").ToList();
            total = list.AsQueryable<UserCirculate>().Where(whereLambda).Count();
            if (isAsc)
            {
                return list.AsQueryable<UserCirculate>().Where(whereLambda)
                    .OrderBy<UserCirculate, S>(orderByLambda)
                    .Skip(pageSize * (pageIndex - 1))
                    .Take(pageSize);
            }
            else
            {
                return list.AsQueryable<UserCirculate>().Where(whereLambda)
                      .OrderBy<UserCirculate, S>(orderByLambda)
                      .Skip(pageSize * (pageIndex - 1))
                      .Take(pageSize);
            }
        }
        #endregion
        #region 04，更新+override bool Update(UserCirculate t)
        /// <summary>
        ///更新
        /// </summary>
        /// <param name="t">实体对象</param>
        /// <returns></returns>
        public override bool Update(UserCirculate t)
        {
            IDictionary<string, object> parameter = new Dictionary<string, object>() { };
            #region MyRegion
            parameter.Add("ZDJYCS", t.ZDJYCS);
            parameter.Add("ZSJYTS", t.ZSJYTS);
            parameter.Add("JYTS", t.JYTS);
            parameter.Add("XJCS", t.XJCS);
            parameter.Add("XJTS", t.XJTS);
            parameter.Add("YYCS", t.YYCS);
            parameter.Add("YYTS", t.YYTS);
            parameter.Add("SJDW", t.SJDW);
            parameter.Add("BLCS", t.BLCS);
            parameter.Add("TSLXDM", t.TSLXDM);
            parameter.Add("DCDDM", t.DCDDM);
            parameter.Add("DZLX", t.DZLX);
            parameter.Add("GCDWDM", t.GCDWDM);
            parameter.Add("GQFJ", t.GQFJ);
            parameter.Add("DZDWMC", t.DZDWMC);
            parameter.Add("MKLX", t.MKLX);
            parameter.Add("YL1", t.YL1);
            #endregion
            return DB.ExecuteNonQuery("Lis.UserCirculate.Update.OneByIdAndCollection", parameter) > 0;
        }
        #endregion

        #region 05，删除+override bool Delete(UserCirculate t)
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t">实体对象</param>
        /// <returns></returns>
        public override bool Delete(UserCirculate t)
        {
            IDictionary<string, object> parameter = new Dictionary<string, object>() { };
            parameter.Add("DZLX", t.DZLX);
            parameter.Add("DCDDM", t.DCDDM);

            return DB.ExecuteNonQuery("Lis.UserCirculate.Delete.OneByIdAndCollection", parameter) > 0;
        }
        #endregion
    }
}
